/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import mapSystem.MapLoader;

public class ImageHandler {
	public static BufferedImage loadImage(String apath)
	{
		BufferedImage img = null;
		
		InputStream imageLoader = MapLoader.class.getResourceAsStream(apath);
        if(imageLoader == null)
        {
            System.out.println("Imagem "+apath+" nao encontrada");
            return null;
        }
        try
        {
            img = ImageIO.read(imageLoader);
        }catch(IOException ioe)
        {
            System.out.println("Imagem "+apath+" nao pode ser carregada");
            return null;
        }
        try {
            /* Fechar stream de imagem */
            imageLoader.close();
        } catch (IOException ex) {}
		
		return img;
	}
    public static BufferedImage addColorKey(BufferedImage old)
    {
        return addColorKey(old, new Color(0, 255, 255).getRGB());
    }
    public static BufferedImage addColorKey(BufferedImage old, int RGB)
    {
        BufferedImage finalImage = null;
        if(old != null)
        {
	        finalImage = new BufferedImage(old.getWidth(), old.getHeight(), BufferedImage.TRANSLUCENT);
	        
	        Graphics2D g = finalImage.createGraphics();
	        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	        g.drawImage(old, 0, 0, null);
	        for(int i = 0; i < old.getWidth(); i++)
	        {
	            for(int j = 0; j < old.getHeight(); j++)
	            {
	                if(old.getRGB(i, j) == RGB)
	                {
	                    finalImage.setRGB(i, j, Color.TRANSLUCENT);
	                }
	            }
	        }
        }
        
        return finalImage;
    }
    public static BufferedImage rotateCW(BufferedImage old)
    {
    	BufferedImage finalImage = null;
    	
    	if(old == null) return finalImage;
    	
    	int width = old.getHeight(); // thats right, really
    	int height = old.getWidth();
    	
    	finalImage = new BufferedImage(width, height, old.getType());
    	
    	for(int i = 0; i < old.getWidth(); i++)
    	{
    		for(int j = 0; j < old.getHeight(); j++)
    		{
    			finalImage.setRGB(finalImage.getWidth()-j-1, i, old.getRGB(i, j));
    		}
    	}
    	
    	return finalImage;
    }
    public static BufferedImage rotateCCW(BufferedImage old)
    {
    	BufferedImage finalImage = null;
    	
    	if(old == null) return finalImage;
    	
    	int width = old.getHeight(); // thats right, really
    	int height = old.getWidth();
    	
    	finalImage = new BufferedImage(width, height, old.getType());
    	
    	for(int i = 0; i < old.getWidth(); i++)
    	{
    		for(int j = 0; j < old.getHeight(); j++)
    		{
    			finalImage.setRGB(j, finalImage.getHeight()-i-1, old.getRGB(i, j));
    		}
    	}
    	
    	return finalImage;
    }
	public static BufferedImage flipHorizontally(BufferedImage old) {
		
		BufferedImage finalImage = null;
    	
    	if(old == null) return finalImage;
    	
    	finalImage = new BufferedImage(old.getWidth(), old.getHeight(), old.getType());
    	
    	for(int i = 0; i < old.getWidth(); i++)
    	{
    		for(int j = 0; j < old.getHeight(); j++)
    		{
    			finalImage.setRGB(finalImage.getWidth() - i - 1, j, old.getRGB(i, j));
    		}
    	}
		return finalImage;
	}
	public static BufferedImage flipVertically(BufferedImage old) {
		
		BufferedImage finalImage = null;
    	
    	if(old == null) return finalImage;
    	
    	finalImage = new BufferedImage(old.getWidth(), old.getHeight(), old.getType());
    	
    	for(int i = 0; i < old.getWidth(); i++)
    	{
    		for(int j = 0; j < old.getHeight(); j++)
    		{
    			finalImage.setRGB(i, finalImage.getHeight() - j - 1, old.getRGB(i, j));
    		}
    	}
		
		return finalImage;
	}
}
